/**
报修列表页 
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/repair/repairList' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>报修列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input
          size="small"
          v-model="formInline.liaisonName"
          placeholder="输入报修人姓名"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          size="small"
          v-model="formInline.liaisonPhone"
          placeholder="输入联系电话"
        ></el-input>
      </el-form-item>
      <el-form-item label="状态：">
        <el-select
          style="width: 100px"
          size="small"
          v-model="formInline.state"
          placeholder="请选择"
        >
          <el-option
            v-for="type in repairState"
            :label="type.key"
            :value="type.value"
            :key="type.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="宿舍：">
        <el-select
          style="width: 150px"
          size="small"
          v-model="formInline.classifyId"
          placeholder="全部"
        >
          <el-option
            v-for="type in classify"
            :label="type.categoryName"
            :value="type.id"
            :key="type.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="search"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table
      size="small"
      :data="listData"
      highlight-current-row
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
      <!-- <el-table-column align="center" type="selection" width="60">
      </el-table-column> -->
      
      <el-table-column sortable prop="liaisonName" label="报修人" width="130">
        
      </el-table-column>
      <el-table-column
        sortable
        prop="liaisonPhone"
        label="联系电话"
        width="200"
      >
      </el-table-column>
      <el-table-column sortable prop="describe" label="报修内容" width="300">
      </el-table-column>
       <!-- <el-table-column sortable prop="categoryName" label="报修内容" width="300">
      </el-table-column> -->
      <el-table-column sortable prop="address" label="地址描述" width="300">
      </el-table-column>
      <el-table-column sortable prop="createTime" label="报修时间" width="300">
        <template slot-scope="scope">
          <div>{{ scope.row.createTime }}</div>
          <span></span>
        </template>
      </el-table-column>
      <el-table-column fixed="left" prop="editUser" label="状态" width="100">
        <template slot-scope="scope">
          <!-- 根据不同状态展示不同标题 -->
          <el-tag type="" effect="plain" v-if="scope.row.state == 1"
            >未处理</el-tag
          >
          <el-tag type="warning" effect="plain" v-if="scope.row.state == 2"
            >处理中</el-tag
          >
          <el-tag type="success" effect="plain" v-if="scope.row.state == 3"
            >完成</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column fixed="right" align="center" label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button
          type="primary" 
            v-if="scope.row.state == 1"
            size="mini"
            @click="setState(2, scope.row)"
            >去处理</el-button
          >
          <el-button
          type="warning" 
            v-if="scope.row.state == 2"
            size="mini"
            @click="setState(3, scope.row)"
            >完成</el-button
          >
          <el-button
            v-if="scope.row.state == 3"
            size="mini"
            @click="setState(null, scope.row)"
            type="success"
            disabled
            >已完成</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="deleteRow(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>
  </div>
</template>

<script>
// import { GetRepairList } from "@/api/repairMG";
import {
  GetRepairList,
  GetAllClassify,
  repairDelete,
  upState,
} from "../../api/repairMG";
import Pagination from "../../components/Pagination.vue";
export default {
  data() {
    return {
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: "预览",
      classify: [{ categoryName: "全部", id: 0 }],

      repairState: [
        { key: "全部", value: 0 },
        { key: "未处理", value: 1 },
        { key: "处理中", value: 2 },
        { key: "处理完成", value: 3 },
      ],

      formInline: {
        pageNum: 1,
        pageSize: 10,
        liaisonName: "",
        liaisonPhone: "",
        state: 0,
        classifyId: 0,
        token: localStorage.getItem("logintoken"),
      },

      listData: [], //报修列表
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10,
      },
    };
  },
  // 注册组件
  components: {
    Pagination,
  },

  /**
   * 创建完毕,初始化数据
   */

  created() {
    this.getClassifyList();
    this.getRepairList(this.formInline);
  },

  //里面的方法被调用才执行
  methods: {
    //分页获取报修列表
    getRepairList(parameter) {
      this.loading = true;
      /***
       * 调用接口，注释上面模拟数据 取消下面注释
       */
      GetRepairList(parameter)
        .then((res) => {
          this.loading = false;
          if (res.code != 200) {
            this.$message({
              type: "info",
              message: res.msg,
            });
          } else {
            console.log(res);
            this.listData = res.data.items;
            // 分页赋值
            this.pageparm.currentPage = this.formInline.pageNum;
            this.pageparm.pageSize = this.formInline.pageSize;
            this.pageparm.total = res.data.totalNum;
          }
        })
        .catch((err) => {
          this.loading = false;
          this.$message.error("菜单加载失败，请稍后再试！");
        });
    },
    //根据条件查询
    search() {
      this.formInline.pageNum = 1;
      this.getRepairList(this.formInline);
    },
    //获取全部报修分类
    getClassifyList() {
      this.loading = true;
      GetAllClassify()
        .then((res) => {
          this.loading = false;
          if (res.code != 200) {
            this.$message({
              type: "info",
              message: res.msg,
            });
          } else {
            console.log(res);
            this.classify.push(...res.data); //数组中追加数组es6写法
          }
        })
        .catch((err) => {
          this.loading = false;
          this.$message.error("菜单加载失败，请稍后再试！");
        });
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.pageNum = parm.currentPage;
      this.formInline.pageSize = parm.pageSize;
      this.getRepairList(this.formInline);
    },
    // 删除报修记录
    deleteRow(index, row) {
      this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          repairDelete(row.id)
            .then((res) => {
              if (res.code == 200) {
                this.$message({
                  type: "success",
                  message: "记录已删除!",
                });
                this.getRepairList(this.formInline);
              } else {
                this.$message({
                  type: "info",
                  message: res.msg,
                });
              }
            })
            .catch((err) => {
              this.loading = false;
              this.$message.error("报修记录删除失败，请稍后再试！");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //修改报修状态
    setState(state, row) {
      if (state) {
        var param = row.id + "/" + state;
        upState(param)
          .then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "处理成功!",
              });
              this.getRepairList(this.formInline);
            } else {
              this.$message({
                type: "info",
                message: res.msg,
              });
            }
          })
          .catch((err) => {
            this.loading = false;
            this.$message.error("处理失败，请稍后再试！");
          });
      }
    },
  },
};
</script>

<style>
.user-search {
  margin-top: 20px;
}
</style>